var basePath;
$(function(){
	sendCode();
	protocol();
	basePath = $(".basePath").attr("url");
	if(basePath == undefined){
		basePath = $("#contextPath").val();
	}
});
var phone_reg = /^1[3|4|5|7|8]\d{9}$/;
var pa=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
//用户登录
var userLogin= function() {
		var userName = $("#userName").val();
		var password = $("#password").val();
		var forward = $("#forward").val();
		 if($.trim(userName).length<=0){
			 layer.open({
				    content:'请输入手机号码',
				    time: 2
				});
			 return false;
		 }
		 if(!phone_reg.test(userName)){ 
			 layer.open({
				    content:'您输入的手机号码不合法,请重新输入',
				    time: 2
				});
	        return false; 
	     } 
		 if($.trim(password).length<=0){
			 layer.open({
				    content:'请输入您的密码!',
				    time: 2
				});
			 return false; 
		 }
		 var param = {userName:userName,pwd:password};
		 $.ajax({
			 url:'login',
			 async:true,
			 data:param,
			 type:'post',
			 dataType:"json",
			 success:function(data, textStatus){
				 if(data.result.state ==0 && data.result.message == 'user not exists'){
					 layer.open({
						    content:'此账号不存在!',
						    time: 2
					});
				 }
				 if(data.result.state == 1){
					 if(data.result.message == 'userIsNull'){
						 layer.open({
							    content:'手机号或密码不正确',
							    time: 2
							});
					 }else{
						 var next = data.next;
						 layer.open({
							    content:'登录成功!',
							    time: 2
							});
						 var cookie = document.cookie;
						 if(cookie.length > 0){
							 $.ajax({
								 url:basePath+'/common/synCookieResult',
								 async:true,
								 type:'post',
								 dataType:"json",
								 success:function(data, textStatus){
										 var exp = new Date();
										 exp.setTime(exp.getTime() - 1);
										 document.cookie = "HLTX_SHOPCART=0;expires="+exp.toGMTString()+";path=/";
										 
							          if(forward != ''){
											 window.location = basePath+forward;
										 }else{
											 window.location = basePath+"/";
										 }
								 }
							 });  
						 }else{
							 if(forward != ''){
								 window.location = basePath+forward;
							 }else{
								 window.location = basePath+"/";
							 } 
						 } 
					 }
				 }
			 },
			 error:function(XMLHttpRequest, textStatus, errorThrown){
				 layer.open({
					    content:textStatus,
					    time: 2
					});
			 }
		 });
}
	/**发送验证码**/
	var sendCode = function(){
		var bo = true;
				$(".sendCode").on("click",function(){
					$('.sendCode').unbind("click");
					setTimeout(function(){
						if(bo){
							bo =false;
					var userName = $("#userName").val();
					var type = $("#types").val();
					if($.trim(userName).length<=0){
						layer.open({
						    content:'请输入注册时使用的手机号码!',
						    time: 2
						});
						 sendCode();
						 return false; 
					}
					if(!phone_reg.test(userName)){
						 layer.open({
							    content:'你所输入手机号码不合法请仔细检查!',
							    time: 2
							});
						 sendCode();
						 return false; 
					}
					$.ajax({
						url:basePath+'/send',
						data:{userName:userName,type:type},
						type:'post',
						success:function(data){
							if(data.result.state == 0 && data.result.code == '100005'){
								layer.open({
								    content:'此用户已注册!',
								    time: 2
								});
							}
							if(data.result.state == 0 && data.result.code == 'error'){
								layer.open({
								    content:'此用户不存在!',
								    time: 2
								});
							}
							if(data.result.state == 1){
								$("#codeHidden").val(data.result.code);
								countdown(data.result.message);
							}
							if(data.result.state == 0 &&  data.result.code == "0000"){
								layer.open({
								    content:data.result.message,
								    time: 2
								});
							}
							/*if(data.sendNum < 10){
									if(data.result.state == 1 && data.result.code =='100001'){
										var codeHidden=$("#codeHidden").val();
										if(codeHidden != undefined){
											$("#codeHidden").val(data.code);
										}
										countdown(data.result.message,data.sendNum);
									}
							}else{
								$(".ui_btn_success").css({backgroundColor:'#ffffff',color:'#00b18f'});
								$(".ui_btn_success").html('发送次数达到当天限制');
								$(".ui_btn_success").removeClass("sendCode");
							}*/
							sendCode();
						},
						error:function(e){
							 layer.open({
								    content:'获取验证码错误请稍刷新后再试',
								    time: 2
							   });
						}
					});
						}
					},1000)
				});
	}
var seTips = function() {
	$(".tips-box").css({'color':'red'});
	setTimeout(function(){
		$(".tips-box").html('');
	}, 2000)
}
var miao = 120;
var  countdown= function (msg){
		var time=setInterval(function(){
			miao--;
			if(miao<=0){
				window.clearInterval(time);
				sendCode();
				$(".ui_btn_success").css({backgroundColor:'#00b18f',color:'#fff'});
				$(".ui_btn_success").html('获取验证码');
			}else{
				$(".ui_btn_success").html(miao+'秒后重发');
			}
		}, 1000)
}

/**注册**/
var registerBtn = function () {
	var friend = $("#friend").val();
	var userName = $("#userName").val();
	var pasword = $("#password").val();
	var code = $("#code").val();
	if($.trim(userName).length<=0){
		layer.open({
		    content:'请输入手机号码!',
		    time: 2
		});
		 return false; 
	}
	if($.trim(friend).length<=0){
		layer.open({
			content:'请输入推荐人手机号码!',
			time: 2
		});
		return false; 
	}
	if(!phone_reg.test(userName)){
		layer.open({
		    content:'你所输入手机号码不合法请仔细检查!',
		    time: 2
		});
		 return false; 
	}
	if(!phone_reg.test(friend)){
		layer.open({
			content:'你所输入推荐人手机号码不合法请仔细检查!',
			time: 2
		});
		return false; 
	}
	
	if($.trim(pasword).length <= 0) {
		layer.open({
		    content:'请输入密码',
		    time: 2
		});
		return false;
	}
	if(!pa.test(pasword)){
		layer.open({
		    content:'密码有6~20位英文字母/数字或符号组成',
		    time: 2
		});
		return false;
	}
	if($.trim(code).length <=0 ){
		layer.open({
		    content:'请输入验证码',
		    time: 2
		});
		return false;
	}
	var isProtocol = $(".simulation_checkbox_active");
	if(isProtocol.length <= 0 ){
		layer.open({
		    content:'请同意协议后在注册',
		    time: 2
		});
		return false;
	}
	var pamam = {friend:friend,mobile:userName,password:pasword,code:code,type:'register'}
	$.ajax({
		url:basePath+'/friend/register',
		data:pamam,
		type:'post',
		success:function(data){
			if(data.result.code == 'PARENT_NULL'){
				layer.open({
				    content:'推荐人不存在',
				    time: 2
				});
				return false;
			}
			if(data.result.code == 'REFERRER_NULL'){
				layer.open({
				    content:'推荐人必须填写!',
				    time: 2
				});
				return false;
			}
			if(data.result.code == 'e'){
				layer.open({
					content:'注册繁忙稍后重试!',
					time: 2
				});
				return false;
			}
			if(data.result.code == 'expire'){
				layer.open({
				    content:'验证码已无效!',
				    time: 2
				});
				return false;
			}
			if(data.result.code == 'error'){
				layer.open({
					content:'此用户已注册!',
					time: 2
				});
				return false;
			}
			if(data.result.code == 'exist'){
				layer.open({
					content:'注册失败稍后重试!',
					time: 2
				});
				return false;
			}
			if(data.result.code == 'ok'){
				layer.open({
				    content:'恭喜您注册成功',
				    time: 2
				});
//				$.ajax({
//					 url:basePath+'/common/synCookieResult',
//					 async:true,
//					 type:'post',
//					 dataType:"json",
//					 success:function(data, textStatus){
//							 var exp = new Date();
//							 exp.setTime(exp.getTime() - 1);
//							 document.cookie = "HLTX_SHOPCART=0;expires="+exp.toGMTString()+";path=/";
//							 window.location=basePath;
//					 }
//				 });
				window.location=basePath+"/friend/toR";
				return false;
			}
		},
		error:function(data){
			
		}
	});
}

/**验证手机收到验证码是否有效**/
var UpdateNewPwd = function(){
	var userPhone = $("#userName").val();
	var code = $("#code").val();
	var password = $("#types").val();
	if(phone_reg.test(userName)){
		layer.open({
		    content:'你所输入手机号码不合法请仔细检查!',
		    time: 10000
		});
		return false; 
	}
	if(code.length <=0){
		layer.open({
		    content:'请输入手机收到的验证码!',
		    time: 10000
		});
		return false; 
	}
	var params = {mobile:userPhone,code:code,type:password};
	$.ajax({
		url:'toNewsPwd',
		data:params,
		type:'post',
		dataType:'json',
		success:function(data){
				
				if(data.result.state ==1 && data.result.code =='expire'){
					layer.open({
					    content:'验证码失效!',
					    time: 10000
					});
					return false;
				}
				if(data.result.message != undefined && data.result.message == '0001'){
					layer.open({
					    content:'验证码输入错误!',
					    time: 10000
					});
					return false;
				}
				countdown(data.result.message);
				if (data.next != '') {
					window.location =basePath+ data.next;
				}
		},
		error:function(data){
			layer.open({
			    content:data,
			    time: 2
			});
		}
	});
}

var updateNewsPwdIng = function (){
	var userName = $("#userName").val();
	var reg_password = $("#reg_password").val();
	var password = $("#password").val();
	if($.trim(password).lenght <= 0){
		layer.open({
		    content:'请输入新密码',
		    time: 10000
		});
		return false;
	}
	if(!pa.test(password)){
		layer.open({
		    content:'密码不合法请重新设置',
		    time: 10000
		});
		return false;
	}
	
	if($.trim(reg_password).lenght <= 0){
		layer.open({
		    content:'请输入确认密码',
		    time: 10000
		});
		return false;
	}
	if($.trim(reg_password) != $.trim(password)){
		layer.open({
		    content:'你输入密码不一致',
		    time: 10000
		});
		return false;
	}
	var param = {userName:userName,password:password};
	$.ajax({
		url:'updateNewPwd',
		data:param,
		type :'post',
		dataType:'json',
		success:function(data){
			if(data.result.state == 0 ){
				layer.open({
				    content:data.result.message,
				    time: 10000
				});
			}
			window.location =basePath+ data.next;
		},
	});
} 

var protocol = function (){
	form.Checkbox('.reg_notice', {
	    onchange: function(Checkbox, _this) {
	      var is = Checkbox.iSelect,
	        ele = Checkbox.ele;
	      if (_this.id === 'iSelect') {
	        if (!is) {
	          [].forEach.call(ele, function(ele) {
	            ele.querySelector('.simulation_checkbox').classList.remove('simulation_checkbox_active');
	          });
	        } else {
	          [].forEach.call(ele, function(ele) {
	            ele.querySelector('.simulation_checkbox').classList.add('simulation_checkbox_active');
	          });
	        }

	      }
	    }
});	
}

var NumberToTime =  function (time){
	var min = parseInt(time/60);
	var sec = time - (min*60);
	if(min == '0'){
		return sec+"秒";
	}
	if(sec == '0' ){
		return min+"分钟"
	}else{
		
		return min+"分钟"+sec+"秒";
	}
}
